package gui_layer;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.KeyStroke;

@SuppressWarnings("serial")
public abstract class SaleRestockNewWindow extends UniversalTableWindow
{
	protected JLabel price_lbl;
	protected JComboBox<String> set_box;
	protected DynamicSearchComboBoxModel dscbm;
	/**
	 * Create the frame.
	 */
	public SaleRestockNewWindow()
	{
		super();
		make_price_lbls();
	}
	
	private void set_table_params()
	{
		table.setCellSelectionEnabled(true);
		InputMap im = table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
		im.put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0), "delete_from_table");
		im.put(KeyStroke.getKeyStroke(KeyEvent.VK_F12, 0), "save_table_contents");
		ActionMap am = table.getActionMap();
		am.put("delete_from_table", new AbstractAction()
		{
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				delete();
				
			}
		});
		am.put("save_table_contents", new AbstractAction()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				save();
				
			}
		});
		
		set_editable_columns();
	}
	
	private void make_set_fields()
	{
		float[] values = Color.RGBtoHSB(255, 104, 0, null);
		set_box = new JComboBox<String>();
		set_box.setBorder(new RoundedCornerBorder());
		set_box.setEditable(true);
		search_set_panel.add(set_box);
		
		dscbm = new DynamicSearchComboBoxModel(set_box);
		set_box.setModel(dscbm);
		set_box.addItemListener(dscbm);
		
		
		JButton btnSearch = new JButton("Search");
		btnSearch.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		btnSearch.setForeground(Color.WHITE);
		btnSearch.setBackground(Color.getHSBColor(values[0], values[1], values[2]));
		btnSearch.setContentAreaFilled(false);
		btnSearch.setOpaque(true);
		btnSearch.setBorder(new RoundedCornerBorder());
		btnSearch.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { search(); }});
		search_set_panel.add(btnSearch);
	}
	
	protected void make_as_new()
	{
		make_option_buttons_new();
		set_table_params();
		make_set_fields();
	}
	
	protected void make_as_details()
	{
		make_option_buttons_details();
		table.setEnabled(false);
		
		set_box = new JComboBox<String>();
		set_box.setBorder(new RoundedCornerBorder());
		set_box.setEnabled(false);
		set_box.setEditable(true);
		search_set_panel.add(set_box);
	}
	
	protected void make_option_buttons_new()
	{
		float[] values = Color.RGBtoHSB(255, 104, 0, null);
		JButton btnAdd = new JButton("Add");
		btnAdd.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		btnAdd.setForeground(Color.WHITE);
		btnAdd.setBackground(Color.getHSBColor(values[0], values[1], values[2]));
		btnAdd.setContentAreaFilled(false);
		btnAdd.setOpaque(true);
		btnAdd.setBorder(new RoundedCornerBorder());
		btnAdd.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { add_product(); }});
		option_button_panel.add(btnAdd);
		
		JButton btnSave = new JButton("Save");
		btnSave.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		btnSave.setForeground(Color.WHITE);
		btnSave.setBackground(Color.getHSBColor(values[0], values[1], values[2]));
		btnSave.setContentAreaFilled(false);
		btnSave.setOpaque(true);
		btnSave.setBorder(new RoundedCornerBorder());
		btnSave.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { save(); }});
		option_button_panel.add(btnSave);
		
		JButton btnDelete = new JButton("Delete");
		btnDelete.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		btnDelete.setForeground(Color.WHITE);
		btnDelete.setBackground(Color.getHSBColor(values[0], values[1], values[2]));
		btnDelete.setContentAreaFilled(false);
		btnDelete.setOpaque(true);
		btnDelete.setBorder(new RoundedCornerBorder());
		btnDelete.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { delete(); }});
		option_button_panel.add(btnDelete);
	}
	
	protected void make_option_buttons_details()
	{
		float[] values = Color.RGBtoHSB(255, 104, 0, null);
		JButton btnOk = new JButton("OK");
		btnOk.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		btnOk.setForeground(Color.WHITE);
		btnOk.setBackground(Color.getHSBColor(values[0], values[1], values[2]));
		btnOk.setContentAreaFilled(false);
		btnOk.setOpaque(true);
		btnOk.setBorder(new RoundedCornerBorder());
		btnOk.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { quit(); }});
		option_button_panel.add(btnOk);
	}
	
	private void make_price_lbls()
	{
		JLabel lblTotalPrice = new JLabel("Total price:");
		lblTotalPrice.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		lblTotalPrice.setForeground(Color.WHITE);
		display_panel.add(lblTotalPrice);
		
		price_lbl = new JLabel("New label");
		price_lbl.setFont(new Font("Helvetica Neue", Font.BOLD, 17));
		price_lbl.setForeground(Color.WHITE);
		display_panel.add(price_lbl);
	}
	
	protected boolean is_integer(String input)
	{
		if (input == null)
		{
			return false;
		}
		try
		{
			Integer.parseInt(input);
			return true;
		}
		catch(NumberFormatException e)
		{
			return false;
		}
	}
	
	protected boolean is_float(String input)
	{
		if(input == null)
		{
			return false;
		}
		try
		{
			Float.parseFloat(input);
			return true;
		}
		catch(NumberFormatException e)
		{
			return false;
		}
	}
	
	protected abstract void search();
	
	protected abstract void add_product();
	
	protected abstract void save();
	
	protected abstract void delete();
	
	protected abstract void set_editable_columns();
	
	
	private void quit()
	{
		dispose();
	}
	

}
